package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.SystemMsgBean;
import com.hxkcloud.patient.utils.DataUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/13.
 */
public class SysteminfoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<SystemMsgBean> datas;
    private LayoutInflater mInflater;
    private OnItemClickLitener onItemClickLitener;
    private boolean isToday = false;
    private boolean isYesTerday = false;

    public interface OnItemClickLitener{
        void onItemClick(int position);
    }
    public SysteminfoListAdapter(Context mContext, List<SystemMsgBean> datas) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);
    }
    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener)
    {
        this.onItemClickLitener = onItemClickLitener;
    }
    public void setData(List<SystemMsgBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;
        SystemMsgBean bean = datas.get(position);
        mHolder.nameTV.setText(bean.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(Long.valueOf(bean.getTime())));
        try {
            isToday = DataUtils.IsToday(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            isYesTerday = DataUtils.IsYesterday(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (isToday){
            mHolder.tv_date.setText("今天");
        }else if (isYesTerday){
            mHolder.tv_date.setText("昨天");
        }else {
            mHolder.tv_date.setText(date);
        }


        if("CHA2DS2–VASC评分结果".equals(bean.getTitle()))
        {
            try
            {
                JSONObject data = new JSONObject(bean.getIntro());
                mHolder.tv_intro.setText(data.getString("AssessmentReport"));

            } catch (JSONException e)
            {
                e.printStackTrace();
            }

        }else
        {
            mHolder.tv_intro.setText(bean.getIntro());
        }



        if (onItemClickLitener!=null){
            mHolder.linearLayout_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickLitener.onItemClick(position);
                }
            });
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = mInflater.inflate(R.layout.item_systeminfo_layout, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends RecyclerView.ViewHolder {
        private LinearLayout linearLayout_item;
        private TextView nameTV;
        private TextView tv_date;
        private TextView tv_intro;

        public Holder(View view) {
            super(view);
            linearLayout_item = (LinearLayout) view.findViewById(R.id.systeminfo_item_layout_whole);
            nameTV = (TextView) view.findViewById(R.id.systeminfo_item_tv_desciption1);
            tv_date = (TextView) view.findViewById(R.id.systeminfo_item_tv_time);
            tv_intro = (TextView) view.findViewById(R.id.systeminfo_item_tv_desciption);
        }
    }
}
